﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DCTAccess.Business
{
    public enum StringCompareTypes
    {
        StartsWith, NotStartWith,

        EndsWith, NotEndsWith,

        Contains, NotContains,ContainsN,

        Equals, NotEqual, NotEqualThan, EqualThan,

        MatchPattern, NotMatchPattern
    }

    public class StringCompareSQLBuilder
    {
        public static string GetSQL(string fieldName, StringCompareTypes compareType, string value, bool compareToLower)
        {
            if (compareToLower)
            {
                value = string.Format("LOWER({0})", value);
            }

            switch (compareType)
            {
                case StringCompareTypes.ContainsN:
                    return string.Format(" ({0} LIKE N'%' + {1} + '%') ", fieldName, value);

                case StringCompareTypes.EqualThan:
                    return string.Format(" ({0} >= {1}) ", fieldName, value);

                case StringCompareTypes.NotEqualThan:
                    return string.Format(" ({0} <= {1}) ", fieldName, value);

                case StringCompareTypes.Contains:
                    return string.Format(" ({0} LIKE '%' + {1} + '%') ", fieldName, value);

                case StringCompareTypes.EndsWith:
                    return string.Format(" ({0} LIKE '%' + {1}) ", fieldName, value);

                case StringCompareTypes.Equals:
                    return string.Format(" ({0} = {1}) ", fieldName, value);

                case StringCompareTypes.StartsWith:
                    return string.Format(" ({0} LIKE {1} + '%') ", fieldName, value);

                case StringCompareTypes.NotEqual:
                    return string.Format(" ({0} <> {1}) ", fieldName, value);
                default:
                    return string.Empty;
            }
        }

        public static string GetSQL(string fieldName, StringCompareTypes compareType, string paramName)
        {
            return GetSQL(fieldName, compareType, paramName, false);
        }
    }
}
